package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.feign.BehaviorFeign;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        //检查数据
        if (dto.getPageSize() > 50 || dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询行为实体
        ApBehaviorEntry apBehaviorEntry = getEntry(dto);
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //分页查询，默认查询5条数据返回
        IPage page = new Page<>(0, dto.getPageSize());
        IPage<ApUserSearch> searchIPage = page(page, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getId, apBehaviorEntry.getId())
                .eq(ApUserSearch::getStatus, 1));
        return ResponseResult.okResult(searchIPage.getRecords());
    }

    @Autowired
    BehaviorFeign behaviorFeign;

    //获取行为实体
    private ApBehaviorEntry getEntry(UserSearchDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEntryId(user.getId(), dto.getEquipmentId());
        return apBehaviorEntry;
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
        //检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //更新当前搜索记录的状态 status0
        ApUserSearch apUserSearch = getById(dto.getId());
        if (apUserSearch == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        apUserSearch.setStatus(0);
        updateById(apUserSearch);
        return ResponseResult.okResult();
    }

    @Override
    @Async("tackExecutor")
    public void insert(Integer entryId, String searchWords) {
        //查询当前搜索记录
        ApUserSearch apUserSearch = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId)
                .eq(ApUserSearch::getKeyword, searchWords));
        //如果存在 更新状态
        if (apUserSearch != null && apUserSearch.getStatus() == 1) {
            log.info("当前关键字已存在，无需再次保存");
            return;
        } else if (apUserSearch != null && apUserSearch.getStatus() == 0) {
            apUserSearch.setStatus(1);
            updateById(apUserSearch);
            return;
        }
        //如果不存在，保存新的数据
        apUserSearch = new ApUserSearch();
        apUserSearch.setEntryId(entryId);
        apUserSearch.setStatus(1);
        apUserSearch.setKeyword(searchWords);
        apUserSearch.setCreatedTime(new Date());
        save(apUserSearch);
    }
}
